package com.week2.day03.library;

import java.util.Arrays;

public class BookManagerImpl implements BookManager {
    @Override
    public boolean addBook(Book book) {
        if (Menu.num == Menu.library.length) {
            Menu.library = Arrays.copyOf(Menu.library, Menu.library.length + 1);
        }
        Menu.library[Menu.num] = book;
        Menu.num++;
        return false;
    }

    @Override
    public boolean deleteBook(int bookId) {
        if (bookId >= 0 && bookId < Menu.library.length) {
            Book[] book = Arrays.copyOf(Menu.library, Menu.library.length);
            for (int i = bookId; i < Menu.library.length; i++) {
                if (i == Menu.library.length - 1) {
                    Menu.library = Arrays.copyOf(Menu.library, i);
                } else {
                    Menu.library[i] = book[i + 1];
                }
            }
            Menu.num--;
        }
        return false;
    }

    @Override
    public boolean modifyBook(Book book) {
        for (int i = 0; i < Menu.library.length; i++) {
            if (Menu.library[i].getName().equals(book.getName())) {
                Menu.library[i] = book;
                break;
            }
        }
        return false;
    }

    @Override
    public Book selectOne(int bookId) {
        Book book = null;
        if (bookId >= 0 && bookId < Menu.library.length) {
            for (int i = 0; i < Menu.library.length; i++) {
                if (bookId == Menu.library[i].getBookId()) {
                    book = Menu.library[i];
                }
            }
        }
        return book;
    }

    @Override
    public String[] selectAll() {
        String[] strs = new String[Menu.library.length];
        for (int i = 0; i < Menu.library.length; i++) {
            strs[i] += "书名：" + Menu.library[i].getName() + "/位置：" + Menu.library[i].getBookLocation() + "/id：" + Menu.library[i].getBookId();
        }
        return strs;
    }
}
